package com.bosch.in.db;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * This class holds all the DB releated Config info  eg. DbUrl,dbDriver etc.
 * This class is refered in BaseDB Manager to get the url and driver names.
 * The set operion on this class is done in ProdisRMIServer class.
 */
public class DBConfig 
{
	//Constants
//	private static final String KEY_DRIVERS = "drivers";
//	private static final String KEY_LOGFILE = "logfile";
//	private static final String DB_LOG = "OracleConnectionManager.log";
	private static final String DB_PROPERTIES = "dbpool.properties";
	//
	private static String strUrl = "rtp2";
//	private static String strDriver = null;  
//	private static PrintWriter log;
	private  static Properties dbProps = null; 
	
	static
	{
    	InputStream is = null;
           dbProps = new Properties();
       
       try 
       {
            is = DBConfig.class.getResourceAsStream(DB_PROPERTIES);
        }
       catch(Exception f)
       { 
        	is = null;
        	 	
        }
        
        try
        {
        	if(is==null)
        	{
        		is = new FileInputStream(DB_PROPERTIES);
        	}
            dbProps.load(is);
        }
        catch (Exception e) 
        {
            System.err.println("Can't read the properties file. " +
                "Make sure "+DB_PROPERTIES+" is in the CLASSPATH");
       }
//        String logFile = dbProps.getProperty(KEY_LOGFILE, DB_LOG);
        /*try 
        {
            log = new PrintWriter(new FileWriter(logFile, true), true);
        }
        catch (IOException e) 
        {
            System.err.println("Can't open the log file: " + logFile);
            log = new PrintWriter(System.err);
        }*/
    }    
	
		/**
		 * setter method to set the url
		 * @param datasourceName
		 */
		/*private static void setUrl(String datasourceName)
		{
		   strUrl = datasourceName;
		}*/
		
		/**
		 * getter method for url
		 * @return
		 */
		private static String getUrl()
		{
			return strUrl;
		}

	/**
	 * getter method for DB url
	 * @return
	 */
	protected static String getDBUrl()
	{
		String poolName = getUrl();
		return dbProps.getProperty(poolName + ".url");
	}

	/**
	 * getter method for DB driver
	 * @return
	 */
	/*private static String getDBDriver()
	{
		return dbProps.getProperty(KEY_DRIVERS);
	}*/

	/**
	 * getter method for DB url
	 * @return
	 */
	protected static String getDBUserName()
	{
		String poolName = getUrl();
		return dbProps.getProperty(poolName + ".user");
	}

	/**
	 * getter method for DB driver
	 * @return
	 */
	protected static String getDBPassword()
	{
		String poolName = getUrl();
		return dbProps.getProperty(poolName + ".password");
	}

}